<?php

/*
 * 承接DQ、DX、DEDE、等各种PHP、SNS程序的定制开发业务,BUG修复业务！
 * 联系QQ：987501448 WX：lnk0102 
 * 邻客互动 lnksns 高颜值论坛博客、社区圈子、积分商城系统 https://www.lnksns.vip
 */
/**
 *      This is NOT a freeware, use is subject to license terms
 *      应用名称: 邻客互动
 *      应用开发者: 叶子
 *      开发者QQ: 987501448
 *      未经应用程序开发者/所有者的书面许可，不得进行反向工程、反向汇编、反向编译等，不得擅自复制、修改、链接、转载、汇编、发表、出版、发展与之有关的衍生产品、作品等
 *      版权所有: 互娱时代襄阳文化产业有限公司
 */

namespace app\lnk\lib;


use app\lnk\model\CircleModel;
use app\lnk\model\CommentModel;
use app\lnk\model\DynamicModel;
use app\lnk\model\IntegralModel;
use app\lnk\model\UserFollowModel;
use app\lnk\model\DynamicImgModel;
use app\lnk\model\DynamicVideoModel;
use app\lnk\model\DynamicTopicModel;
use app\lnk\model\TopicModel;
use app\lnk\model\UserLikeDynamicModel;
use app\lnk\model\CircleFansModel;
use app\lnk\model\MessageModel;
use app\lnk\model\UserModel;
use app\lnk\service\DynamicService;
use lite\service\ConfigService;
use lite\lib\help;
use think\facade\Db;
use think\Request;

use app\appcenter\lib\AppIHookService;


class Helpapi
{
	public static function save_dynamic($request)
    {
        $uid = $request->uid;
        $params = $request->post();
        $id = $params['id'];
        $model = ['user_id' => $uid, 'content' => $params['content'], 'circle_avatar' => $params['circle_avatar'], 'circle_id' => $params['circle_id'], 'circle_name' => $params['circle'], 'type' => $params['type']];
        if(empty($params['imgs']) && empty($params['video']['url'])) $model['type'] = 0;
        if (!empty($params['adds'])) {
            $model += ['adds' => $params['adds']['name']];
            $model += ['lat' => $params['adds']['latitude']];
            $model += ['lng' => $params['adds']['longitude']];
        }
        $model += ['status' => 0];
        if (!ConfigService::get('lnk', 'lnk_sh_dt', 1)) $model['status'] = 1;
        if ($id) {
            $model += ['update_time' => time()];
            AppIHookService::Hook_Run('edit_dynamic',$model,['uid'=>$uid,'aid'=>$id,'request'=>$request]);
            DynamicModel::where('id', $id)->update($model);
            AppIHookService::Hook_Run('edit_dynamic_out',$model,['uid'=>$uid,'aid'=>$id,'request'=>$request]);
            DynamicImgModel::where('dynamic_id', $id)->delete();
            DynamicVideoModel::where('dynamic_id', $id)->delete();
            DynamicTopicModel::where('dynamic_id', $id)->delete();
        } else {
            $model += ['create_time' => time()];
            $ip = $request->ip();
            $model += ['ip' => $ip];
            $res = help::ip($ip);
            if ($res['status'] == 0) {
                if (empty($params['adds'])) {
                    $model += ['lat' => $res['result']['location']['lat']];
                    $model += ['lng' => $res['result']['location']['lng']];
                }
                $model += ['country' => $res['result']['ad_info']['nation']];
                $model += ['province' => $res['result']['ad_info']['province']];
                $model += ['city' => $res['result']['ad_info']['city']];
                $model += ['district' => $res['result']['ad_info']['district']];
            }
            AppIHookService::Hook_Run('add_dynamic',$model,['uid'=>$uid,'request'=>$request]);
            $id = (new DynamicModel)->insertGetId($model);
            AppIHookService::Hook_Run('add_dynamic_out',$model,['uid'=>$uid,'aid'=>$id,'request'=>$request]);

            // 通知圈主
            if ($params['circle_id']) {
                $cinfo = CircleModel::where('id', $params['circle_id'])->field('user_id,avatar')->find();
                if ($cinfo && !empty($cinfo['user_id'])) {
                    $msg = '你的圈子【' . $params['circle'] . '】有一条新的内容待审核，请及时处理。';
                    $img = $cinfo['avatar'];
                    $content_url = '/pages/center/examine?id=' . $params['circle_id'] . '&name=' . $params['circle'];
                    (new MessageModel())->send($cinfo['user_id'], '新内容待审核', $msg, 1, $img, '', $content_url, 0);
                }
            }
        }
        if ($params['type'] == 1 && !empty($params['imgs'])) {
            $img_model = [];
            foreach ($params['imgs'] as $k => $v) {
                $img_model[$k]['url'] = $v['url'];
                $img_model[$k]['wide'] = $v['wide'];
                $img_model[$k]['high'] = $v['high'];
                $img_model[$k]['weigh'] = $k;
                $img_model[$k]['dynamic_id'] = $id;
                $img_model[$k]['user_id'] = $uid;
                $img_model[$k]['status'] = 1;
            }
            DynamicImgModel::insertAll($img_model);
        }
        if ($params['type'] == 2 && !empty($params['video'])) {
            DynamicVideoModel::insert([
                'url' => $params['video']['url'],
                'img' => $params['video']['img'],
                'wide' => $params['video']['wide'],
                'high' => $params['video']['high'],
                'dynamic_id' => $id,
                'user_id' => $uid,
                'update_time' => time()
            ]);
        }
        // if (!empty($params['topic'])) {
        //     $topic_model = [];
        //     foreach ($params['topic'] as $k => $v) {

        //         $topic_data = TopicModel::where('name',$v)->find();
        //         if(empty($topic_data)){
        //             $model_topic = ['user_id'=>$uid,'name'=>$v];
        //             $topic_id = (new TopicModel)->insertGetId($model_topic);
        //         }else{
        //             $topic_id = $topic_data['id'];
        //         }
        //         $topic_model[$k]['dynamic_id'] = $id;
        //         $topic_model[$k]['topic_id'] = $topic_id;
        //         $topic_model[$k]['name'] = $v;
        //     }
        //     DynamicTopicModel::insertAll($topic_model);
        // }
        CircleModel::where('id', $params['circle_id'])->update(['update_time' => time()]);
        if($model['status']==0){
            return success('发布成功,待管理员审核 🎉', '');
        }else{
            return success('发布成功 🎉', '');
        }
        
    }

}